﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Logic.Company;
using Job.Entity;
using Job.Web.Framework;

namespace Job.Web.userControls.Company
{
    public partial class PositionList : BaseCtrl
    {
        private int CompanyId
        {
            get
            {
                //后台管理员能通过后台为企业增加职位，所以在此处需要兼职企业ID从url或从Session中获取
                int id = myR["company"].IntValue;
                if (id > 0) return id;
                MySession session = new MySession();
                SessionCompany sCompany = session.GetCompanyFromSession();
                if (null == sCompany)
                    return 0;
                else
                    return sCompany.CompanyId;
            }
        }

        PositionLogic logic = new PositionLogic();

        protected void Page_Load(object sender, EventArgs e)
        {
            msgTab.Visible = false;
            if (0 == CompanyId)
            {
                Response.Redirect("CompanyLogin.aspx?categoryid=2&error=1");
                return;
            }

            if (!IsPostBack)
            {
                BindCount();
                BindData();
            }
        }

        private void BindData()
        {
            try
            {
                int pageSize = pagerButtom.PageSize;
                int pageIdx = pagerButtom.CurrentPageIndex;
                DataTable dt = logic.GetAdminPagePositionByCompany(CompanyId,pageSize,pageIdx);
                if (dt == null)
                {
                    ShowError("未查询到职位列表！");
                }
                else
                {
                    this.CompanyPosList.DataSource = dt;
                    this.CompanyPosList.DataBind();
                }
            }
            catch (Exception ex)
            {
                this.CompanyPosList.Visible = false;
                ShowError("查询职位列表失败！");
            }
            
        }

        private void BindCount()
        {
            try
            {
                int count = logic.GetPositionCountByCompany(this.CompanyId);
                if (count > 0)
                {
                    pagerButtom.RecordCount = count;
                    pagerButtom.DataBind();
                    pagerButtom.Visible = true;
                }
                else
                {
                    pagerButtom.Visible = false;
                }

            }
            catch (Exception ex)
            {
                this.CompanyPosList.Visible = false;
                ShowError("查询职位列表失败！");
            }
        }

        private void ShowError(string message)
        {
            this.CompanyPosList.Visible = false;
            this.msgTab.Visible = true;
            this.msgTd.InnerHtml = "<font color='#990000'>" + message + "</font>";
            this.pagerButtom.Visible = false;
        }

        protected void CompanyPosList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex > -1)
                {
                    try 
                    {
                        LinkButton lbtDel = (LinkButton)e.Row.FindControl("lbtDelete");
                        if (lbtDel != null) lbtDel.Attributes.Add("onClick", "return confirm('是否确认删除？')");

                        Label lbId = (Label)e.Row.FindControl("lbId");
                        HyperLink hlPosition = (HyperLink)e.Row.FindControl("hlPosition");
                        string url = WebCommon.GetApplicationPath(Request)
                            + "Company/CompanyManager.aspx?categoryid=2&subjectid=" 
                            + ((int)Constants.CompanySubject.JobPosition).ToString()
                            + "&edit=1&position=" + lbId.Text.Trim()
                            + "&company=" + CompanyId;
                        hlPosition.NavigateUrl = url;
                    }
                    catch(Exception ex)
                    {}
                }
            }
        }


        protected void CompanyPosList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int row = e.RowIndex;
            Label label = (Label)CompanyPosList.Rows[row].FindControl("lbId");
            string positionId = label.Text;
            if ( null == positionId || positionId.Trim().Equals("")) return;
            logic.Delete(CompanyId,Convert.ToInt32(label.Text.Trim()));

            BindCount();
            BindData();
        }

        protected void CompanyPosList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int row = e.NewEditIndex;
            HyperLink hlPosition = (HyperLink)CompanyPosList.Rows[row].FindControl("hlPosition");
            if (null == hlPosition) return;

            string url = hlPosition.NavigateUrl;
            if (null == url || url.Trim().Equals("")) return;
            Response.Redirect(url);
        }

        protected void btNew_Click(object sender, EventArgs e)
        {
            string url = "CompanyManager.aspx?categoryid=2&subjectid="
                + ((int)Constants.CompanySubject.JobPosition).ToString()
                + "&edit=1&position=0&company=" + CompanyId;
            Response.Redirect(url);
        }

        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}